package com.sfeir.gwt.webding.client.showcase;

import java.util.Arrays;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sfeir.gwt.webding.client.component.label.LabelTextBox;
import com.sfeir.gwt.webding.client.component.validator.AlphaNumericValidator;
import com.sfeir.gwt.webding.client.component.validator.AlphaValidator;
import com.sfeir.gwt.webding.client.component.validator.InArrayValidator;
import com.sfeir.gwt.webding.client.component.validator.MandatoryValidator;
import com.sfeir.gwt.webding.client.component.validator.NumericValidator;
import com.sfeir.gwt.webding.client.component.validator.StringLengthValidator;
import com.sfeir.gwt.webding.client.component.validator.Validator;
import com.sfeir.gwt.webding.client.showcase.ShowcaseAnnotations.ShowcaseSource;


public class TabValidator extends LazyTab {
    /**
     * 
     */
    @Override
    @ShowcaseSource
    protected Widget createTab() {
        VerticalPanel panel = new VerticalPanel();

        LabelTextBox field1 = new LabelTextBox("Champ obligatoire");
        field1.setMandatory(true);
        panel.add(field1);

        LabelTextBox field2 = new LabelTextBox("Validateur Mandatory");
        field2.addValidator(new MandatoryValidator<String>());
        panel.add(field2);

        LabelTextBox field3 = new LabelTextBox("Validateur Mandatory avec message personalisé");
        field3.addValidator(new MandatoryValidator<String>("Obligatoire"));
        panel.add(field3);

        LabelTextBox field4 = new LabelTextBox("Champ obligatoire avec message personalisé");
        field4.setMandatory(true);
        field4.setMandatoryErrorText("Obligatoire");
        panel.add(field4);

        LabelTextBox field5 = new LabelTextBox("Validation Alpha");
        field5.addValidator(new AlphaValidator());
        panel.add(field5);

        LabelTextBox field6 = new LabelTextBox("Validation nombre");
        field6.addValidator(new NumericValidator());
        panel.add(field6);

        LabelTextBox field7 = new LabelTextBox("Validation Alphanumérique");
        field7.addValidator(new AlphaNumericValidator());
        panel.add(field7);

        LabelTextBox field8 = new LabelTextBox("Validation par une liste de valeurs (a, b ou c)");
        field8.addValidator(new InArrayValidator<String>(Arrays.asList("a", "b", "c")));
        panel.add(field8);

        LabelTextBox field9 = new LabelTextBox("Longueur de la chaine");
        field9.addValidator(new StringLengthValidator(3, 3));
        panel.add(field9);

        LabelTextBox field10 = new LabelTextBox("Validateur perso");
        field10.addValidator(new Validator<String>() {
            @Override
            public String validate(String value) {
                if (value.startsWith("a"))
                    return "Ne doit pas commencer par la lettre a";
                return null;
            }
        });
        panel.add(field10);

        return panel;
    }

    @Override
    public String getDescription() {
        return "Validateurs de champs";
    }

    @Override
    public String getName() {
        return "Validateurs";
    }
}
